﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmSafetyDevice : Form
    {
        bool isChange = false;
        SafetyDeviceInfo _SafetyDeviceInfo;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        SafetyDeviceClient wcfSafetyDevice;
        EmployeeInfo _EmployeeInfo;
        EmployeeClient _wcfEmployee;
        int _EmployeeID;
        public frmSafetyDevice(int SafetyDeviceID, int EmployeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            //Langugage();
            dlgResult = DialogResult.Cancel;
            cboType.SelectedIndex = 0;
            _SafetyDeviceInfo = new SafetyDeviceInfo();
            _EmployeeInfo = new EmployeeInfo();
            wcfSafetyDevice = Service.SafetyDevice();
            _wcfEmployee = Service.Employee();
            _SafetyDeviceInfo.ID = SafetyDeviceID;
            UISetting.LoadBranch(ref cboBranch);
            UISetting.LoadDepartment(ref cboDepartment, Convert.ToInt32(cboBranch.SelectedValue));
            mtxDate.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
            if (SafetyDeviceID > 0)
            {
                _SafetyDeviceInfo = wcfSafetyDevice.SafetyDevice_GetInfo(SafetyDeviceID);
                Info2Form();
                if (_SafetyDeviceInfo.EmployeeID > 0)
                    GetEmployeeName(_SafetyDeviceInfo.EmployeeID);
            }
            _EmployeeID = EmployeeID;
            if(EmployeeID>0)
            {
                GetEmployeeName(EmployeeID);
                btnSelectEmp.Visible = false;               
                cboType.Enabled = false;
                //cboBranch.Visible = false;
                //cboDepartment.Visible = false;
            }
            else
                btnSelectEmp.Visible = true;

            listRole = _listRole;
            isChange = false;
        }

        private void Langugage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }
        void GetEmployeeName(int employeeid)
        {
            _EmployeeInfo = _wcfEmployee.Employee_GetInfo(employeeid);
            txtEmpName.Tag = _EmployeeInfo.ID;
            txtEmpName.Text = _EmployeeInfo.LastName + " " + _EmployeeInfo.FirstName;
        }
        void Info2Form()
        {
            txtName.Text = _SafetyDeviceInfo.Name;
            if (_SafetyDeviceInfo.Date != DateTime.MinValue)
            {
                mtxDate.Text = _SafetyDeviceInfo.Date.ToString("dd/MM/yyyy");
            }
            else
            {
                mtxDate.Text = "";
            }
            txtNumber.Text = _SafetyDeviceInfo.No.ToString();
            txtStatus.Text = _SafetyDeviceInfo.Status;
            txtEmpName.Tag = _SafetyDeviceInfo.EmployeeID;
            cboBranch.SelectedValue = _SafetyDeviceInfo.BranchID;
            cboDepartment.SelectedValue = _SafetyDeviceInfo.DepartmentID;
            cboType.Text = _SafetyDeviceInfo.Type;
            
            isChange = false;
        }

        void Form2Info()
        {
            _SafetyDeviceInfo.EmployeeID = _EmployeeInfo.ID;
            _SafetyDeviceInfo.BranchID = Convert.ToInt32(cboBranch.SelectedValue);
            _SafetyDeviceInfo.DepartmentID = Convert.ToInt32(cboDepartment.SelectedValue);
            _SafetyDeviceInfo.Date = UISetting.GetDate(mtxDate.Text);
            _SafetyDeviceInfo.Name = txtName.Text.Trim();
            _SafetyDeviceInfo.No = UISetting.GetIntNo(txtNumber.Text);
            _SafetyDeviceInfo.Status = txtStatus.Text;
            _SafetyDeviceInfo.Type = cboType.Text;
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (!CheckTextValid.IsDateTime(mtxDate.Text, "dd/MM/yyyy"))
            {
                MessageBox.Show(UpdateUI.GetString("ngaygiaokhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                mtxDate.Focus();
                return false;
            }
            if (cboType.SelectedIndex == 0)
            {
                if (Convert.ToInt32(txtEmpName.Tag) == 0)
                {
                    MessageBox.Show(UpdateUI.GetString("chuachonnhanvien"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtEmpName.Focus();
                    return false;
                }
            }
            else
            {
                if (Convert.ToInt32(cboBranch.SelectedValue) == 0)
                {
                    MessageBox.Show(UpdateUI.GetString("chuachonchinhanh"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtEmpName.Focus();
                    return false;
                }
                if (Convert.ToInt32(cboDepartment.SelectedValue) == 0)
                {
                    MessageBox.Show(UpdateUI.GetString("chuachonphongban"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtEmpName.Focus();
                    return false;
                }
            }
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show(UpdateUI.GetString("tenthietbikhongduoctrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtName.Focus();
                return false;
            }

            if (!CheckTextValid.IsInteger(txtNumber.Text))
            {
                MessageBox.Show(UpdateUI.GetString("soluongkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtNumber.Focus();
                return false;
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_SafetyDeviceInfo.ID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfSafetyDevice.SafetyDevice_Update(_SafetyDeviceInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _SafetyDeviceInfo.ID = wcfSafetyDevice.SafetyDevice_Insert(_SafetyDeviceInfo);
                if (_SafetyDeviceInfo.ID > 0)
                {

                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            _SafetyDeviceInfo.ID = 0;
            cboType.Text = "";
            cboBranch.SelectedValue = 0;
            cboDepartment.SelectedValue = 0;
            txtName.Text = "";
            mtxDate.Text = "";
            txtNumber.Text = "0";
            txtStatus.Text = "";
            txtEmpName.Tag = 0;
            txtEmpName.Text = "";
        }

        private void frmSafetyDevice_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            wcfSafetyDevice.Close();
            this.DialogResult = dlgResult;
        }

        private void frmSafetyDevice_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboType.SelectedIndex == 0)
            {
                cboBranch.SelectedValue = 0;
                cboDepartment.SelectedValue = 0;
                cboBranch.Enabled = false;
                cboDepartment.Enabled = false;
                btnSelectEmp.Enabled = true;
            }
            else
            {
                btnSelectEmp.Enabled = true;
                cboBranch.Enabled = true;
                cboDepartment.Enabled = true;
            }
        }

        private void btnSelectEmp_Click(object sender, EventArgs e)
        {
            frmEmployeeList f = new frmEmployeeList(true);
            if (f.ShowDialog() == DialogResult.OK)
            {
                GetEmployeeName(f.EmpID);
            }
        }

        private void cboBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            UISetting.LoadDepartment(ref cboDepartment, Convert.ToInt32(cboBranch.SelectedValue));
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
